package com.panda.controller;/**
 * @author 乐于吕
 * * @date 2024/10/16
 */


import cn.hutool.json.JSONUtil;
import com.panda.client.HystrixClient;
import com.panda.client.RoleClient;
import com.panda.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 乐于吕
 * @CreateTime: 2024-10-16
 * @Description: 如何通过openfeign调用其他服务，实现服务间的通信
 * 1、导包
 * 2、配置参数
 * 3、添加注解
 */


@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private RoleClient roleClient;

    @Autowired
    private HystrixClient hystrixClient;

    @GetMapping("m1")
    public String method1() {
        String roleInfo = roleClient.method1();
        return "userController method1 success -->  roleController" + roleInfo;
    }


    @GetMapping("m2")
    public String method2(Integer id) {
        String roleInfo = roleClient.method2(id);
        return "userController method2 success -->  roleController" + roleInfo;
    }

    @GetMapping("m3/{id}")
    public String method3(@PathVariable("id") Integer id) {
        String roleInfo = roleClient.method3(id);
        return "userController method3 success -->  roleController" + roleInfo;
    }

    //    http://localhost:8544/role/m4?id=1&roleName=admin$description=管理员
    @GetMapping("m4")
    public String method4() {
        Role role = new Role();
        role.setId(1);
        role.setRoleName("admin");
        role.setDescription("管理员");
        String roleInfo = roleClient.method4(role.getId(), role.getRoleName(), role.getDescription());
        return "userController method4 success -->  roleController" + roleInfo;
    }

    @GetMapping("m5")
    public String method5() {
        Role role = new Role();
        role.setId(1);
        role.setRoleName("admin");
        role.setDescription("管理员");
        String roleInfo = roleClient.method5(role);
        return "userController method5 success -->  roleController" + roleInfo;
    }

    @GetMapping("m6")
    public String method6() {
        String[] ids = {"1", "2", "3", "4"};
        String roleInfo = roleClient.method6(ids);
        return "userController method6 success -->  roleController" + roleInfo;
    }

    @GetMapping("m71")
    public String method71() {
        String roleInfo = roleClient.method71();
        Role role = JSONUtil.toBean(roleInfo, Role.class);
        return "userController method71 success -->  roleController" + role;
    }

    @GetMapping("m72")
    public String method72() {
        Role roleInfo = roleClient.method72();
        return "userController method72 success -->  roleController   " + "  " + roleInfo.getId() + "  " + roleInfo.getRoleName() + "  " + roleInfo.getDescription();
    }

    @GetMapping("m8")
    public String method8() {
        List<Role> roles = roleClient.method8();
        roles.forEach(System.out::println);
        return " userController method8  success ---> roleController";
    }

    @GetMapping("m9")
    public String method9(Integer id) {
        String hystrixMessage = hystrixClient.method1(id);
        return " userController method9  success ---> HystrixController   hystrixMessage ：  " + hystrixMessage;
    }


}


